import openai

# 配置API密钥和基础URL
api_key = "sk-ThA0WI5NVgQ1xYUf8BriyRJfIqBWRvbV5rrzmAWBvdjxsnWP"
base_url = "https://yibuapi.com/v1"


def get_openai_response(question, api_key, base_url):
    """向OpenAI API发送请求并获取响应"""
    try:
        client = openai.OpenAI(api_key=api_key, base_url=base_url)
        response = client.chat.completions.create(
            model="gpt-4-turbo",
            messages=[{"role": "user", "content": question}]
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"请求失败: {str(e)}"


def main():
    print("欢迎使用AI助手！输入问题按回车提交，输入'exit'退出。")

    while True:
        user_input = input("\n请输入您的问题：").strip()

        if user_input.lower() == 'exit':
            print("感谢使用，再见！")
            break

        if not user_input:
            print("问题不能为空，请重新输入。")
            continue

        response = get_openai_response(user_input, api_key, base_url)
        print(f"\nAI回答：\n{response}\n")


if __name__ == "__main__":
    main()